import FixScreen from "../gameEvent/autoFixed";
import DogerFixed from "../dogoer/DogoerFixMode";
import { Constant } from "../constant/Constant";
import AddScoreNode from "../colider/AddScore";
import NumberShow from "../number/NumberShow";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class EventSystem extends cc.Component {

    @property(cc.Node)
    gameStartObj: cc.Node = null;//开始游戏界面

    @property(cc.Node)
    slider: cc.Node = null;//游戏物体

    @property(cc.Node)
    reStartObj: cc.Node = null//结束游戏界面

    @property(cc.Node)
    bgNode: cc.Node = null;//背景图

    @property(cc.Node)
    scoreShow: cc.Node = null;//分数显示

    @property(cc.Node)
    scoreAdder: cc.Node = null;//积分器

    @property(cc.Node)
    faildGameScoreShow: cc.Node = null;//游戏失败分数计数器

    /**
     * 障碍块
     */
    @property(cc.Node)
    twoSideDog: cc.Node = null;//两边大障碍

    @property(cc.Node)
    middleDog: cc.Node = null;//中间大障碍

    @property(cc.Node)
    leftDog: cc.Node = null;//左边大障碍

    @property(cc.Node)
    rightDog: cc.Node = null;//右边大障碍

    public produceSpeed = 2.5;

    public gaming: boolean = false;//游戏进行标志


    /**
     * 随机生成飞机
     */
    public randomGetPlane() {
        let t = this;
        let rand = parseInt((Math.random() * 4) + "");
        switch (rand) {
            case 0:
                t.activeOneOfArr(t.twoSideArr);
                break;
            case 1:
                t.activeOneOfArr(t.middArr);
                break;
            case 2:
                t.activeOneOfArr(t.rightBigArr);
                break;
            case 3:
                t.activeOneOfArr(t.leftBigArr);
                break;
            default:
                // t.twoSideDog.active = true;
                break;
        }
    }


    /**
     * 开始游戏按钮
     */
    private startGame() {
        this.gameStartObj.active = false;
        this.reStartObj.active = false;
        this.enableAllGamingObj();
        this.gaming = true;
        Constant.NOW_SCORE = 0;//当前分数清零
    }
    /**
     * 结束游戏按钮
     */
    private endGame() {
        cc.game.end();
    }
    /**
     * 游戏开始画面
     */
    mainMenu() {
        this.gameStartObj.active = true;
        this.disableAllGamingObj();
    }
    /**
     * 游戏结束画面
     */
    gameFailed() {
        this.gaming = false;
        this.reStartObj.active = true;
        let lastNum = Constant.NOW_SCORE;
        this.scoreShow.getChildByName("ScoLayer").getComponent(NumberShow).showNumber(0);//分数清零
        this.faildGameScoreShow.getComponent(NumberShow).showNumber(lastNum);
        this.disableAllGamingObj();
    }

    /**
     * 将所有游戏物体失效
     */
    private disableAllGamingObj() {
        let t = this;
        Constant.GAME_SPEED = 20;
        t.slider.active = false;
        t.scoreShow.active = false;
        t.resetAll();
        cc.director.getCollisionManager().enabled = false;//关闭物理系统
        //cc.director.getCollisionManager().enabledDebugDraw = false;//关闭碰撞盒阴影
        cc.log("物理系统关闭");
    }

    /**
     * 将所有的游戏物体生效
     */
    private enableAllGamingObj() {
        cc.director.getCollisionManager().enabled = true;//开启物理系统
        //  cc.director.getCollisionManager().enabledDebugDraw = true;//显示碰撞盒阴影
        cc.log("物理系统开启");
        this.slider.active = true;
        this.scoreShow.active = true;
    }

    /**
     * 改变游戏速度
     * @param speed
     */
    public changeSpeed(speed: number) {
        Constant.GAME_SPEED = speed;
        this.produceSpeed = 50 / Constant.GAME_SPEED;
    }

    /**
     * 系统垃圾回收
     */
    private gc() {
        cc.sys.garbageCollect();
        cc.log("垃圾回收完毕");
    }

    onLoad() {
    }
    private middArr: cc.Node[] = [];
    private leftBigArr: cc.Node[] = [];
    private rightBigArr: cc.Node[] = [];
    private twoSideArr: cc.Node[] = [];
    /**
     * 重置所有的节点
     */
    private resetAll() {
        this.resetAllNode(this.middArr);
        this.resetAllNode(this.leftBigArr);
        this.resetAllNode(this.rightBigArr);
        this.resetAllNode(this.twoSideArr);
    }
    //单独接口
    private resetAllNode(arr: cc.Node[]) {
        arr.forEach((e) => {
            e.y = Constant.SCREEN.height / 2;
            e.active = false;
        });
    }
    /**
     * 激活未被激活的节点
     * @param arr 
     */
    private activeOneOfArr(arr: cc.Node[]) {
        for (let index = 0; index < arr.length; index++) {
            if (arr[index].active) continue;
            arr[index].active = true;
            break;
        }
    }
    /**
     * 复制节点存入集合
     * @param temp 
     * @param arr
     */
    private fill(temp: cc.Node, arr: cc.Node[]) {
        let canvas = cc.find("Canvas");
        for (let i = 0; i < 5; i++) {
            let copy = cc.instantiate(temp);
            copy.parent = canvas;
            copy.active = false;
            arr.push(copy);
        }
        arr.push(temp);
    }
    start() {
        let t = this;
        //TODO:复制节点存入集合
        t.fill(t.leftDog, t.leftBigArr);
        t.fill(t.rightDog, t.rightBigArr);
        t.fill(t.middleDog, t.middArr);
        t.fill(t.twoSideDog, t.twoSideArr);
        //END
        t.gameStartObj.active = true;
        t.changeSpeed(Constant.GAME_SPEED);
        this.schedule(() => {//游戏开始时随机产生飞机
            if (t.gaming) {
                t.randomGetPlane();
            }
        }, t.produceSpeed);
        setInterval(() => {//系统垃圾回收间隔60 S
            t.gc();
        }, 60000);//60s一次GC
        //TODO:后台切换监听
        cc.game.on(cc.game.EVENT_HIDE, function () {
            console.log("游戏进入后台");
            cc.game.pause();
        }, t);
        cc.game.on(cc.game.EVENT_SHOW, function () {
            console.log("重新返回游戏");
            cc.game.resume();
        }, t);
    }
    // private speed: number = 1;
    // update(dt) {
    //     this.changeSpeed(this.speed++);
    // }
}
